package com.ebt.m.proposal_v2.widget.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.utils.ActivityUtils;
import com.ebt.m.commons.widgets.BaseRxDialogFragment;
import com.ebt.m.widget.EBTProgress;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @author archer.qi
 *         Created on 2017/5/16.
 */
public abstract class EBTBottomDialogBase extends BaseRxDialogFragment {
    public static final String Arg_Data = "Data";
    public View mContentView;

    @BindView(R.id.root)
    RelativeLayout mRooterView;

    @BindView(R.id.dialog_header)
    RelativeLayout mDialogHeader;
    @BindView(R.id.dialog_header_title)
    TextView mDialogHeaderTitle;
    @BindView(R.id.dialog_header_right)
    ImageView mDialogHeaderClose;
    @BindView(R.id.dialog_header_divider)
    View mHeaderDivider;

    @BindView(R.id.dialog_footer)
    LinearLayout mDialogFooter;
    @BindView(R.id.dialog_footer_negative)
    Button mFooterBtnNegative;
    @BindView(R.id.dialog_footer_positive)
    Button mFooterBtnPositive;
    @BindView(R.id.dialog_footer_divider)
    View mFooterDivider;

    @BindView(R.id.dialog_container)
    LinearLayout mDialogContainer;

    @BindView(R.id.dialog_empty)
    View mDialogEmpty;
    @BindView(R.id.product_empty_logo)
    ImageView mDialogEmptyImage;
    @BindView(R.id.product_empty_content)
    TextView mDialogEmptyContent;
    @BindView(R.id.dialog_progress)
    EBTProgress mDialogProgress;
    @BindView(R.id.empty_container)
    LinearLayout emptyContainer;
    @BindView(R.id.no_net_container)
    RelativeLayout noNetContainer;
    @BindView(R.id.net_slow_container)
    RelativeLayout netSlowContainer;
    @BindView(R.id.load_refresh)
    TextView loadRefresh;
    @BindView(R.id.load_refresh_net)
    TextView loadRefreshNet;

    private String mDialogTitle;
    private String mDialogNegativeText;
    private View.OnClickListener mDialogNegativeListener;
    private String mDialogPositiveText;
    private View.OnClickListener mDialogPositiveListener;
    private boolean isBottomHiden;
    private boolean isTitleHiden;
    private View mContainerChildView;
    private int dialogHeight;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.dialog_fragment_bottom_base, container, false);
            ButterKnife.bind(this, mContentView);
            init();
            bindData();
        }
        if (mContentView != null) {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeAllViews();
            }
        }
        return mContentView;
    }

    private void bindData() {
        if (isTitleHiden) {
            mDialogHeader.setVisibility(View.GONE);
        } else {
            if (mDialogTitle != null && mDialogTitle.length() > 0) {
                mDialogHeaderTitle.setText(mDialogTitle);
            } else {
                mDialogHeader.setVisibility(View.GONE);
            }
        }

        if (isBottomHiden) {
            mDialogFooter.setVisibility(View.GONE);
            mFooterDivider.setVisibility(View.GONE);
        } else {
            if (mDialogNegativeText != null && mDialogNegativeText.length() > 0) {
                mFooterBtnNegative.setText(mDialogNegativeText);
            }
            if (mDialogNegativeListener != null) {
                mFooterBtnNegative.setOnClickListener(mDialogNegativeListener);
            }
            if (mDialogPositiveText != null && mDialogPositiveText.length() > 0) {
                mFooterBtnPositive.setText(mDialogPositiveText);
            }
            if (mDialogPositiveListener != null) {
                mFooterBtnPositive.setOnClickListener(mDialogPositiveListener);
            }
        }

        if (mContainerChildView != null) {
            mDialogContainer.removeAllViews();
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            mDialogContainer.addView(mContainerChildView, params);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        resizeDialog();
    }

    @Override
    public void onStart() {
        super.onStart();
        removeBorder();
    }

    /**
     * 设定DIALOG大小与位置
     */
    private void resizeDialog() {
        Dialog mDialog = getDialog();
        WindowManager.LayoutParams p = mDialog.getWindow().getAttributes();
        p.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogHeight = (int) (ActivityUtils.getScreenSize(getActivity())[1] * 0.6);
        p.height = dialogHeight;
        p.gravity = Gravity.BOTTOM;
        p.windowAnimations = R.style.BottomDialogAnimation;
        mDialog.getWindow().setAttributes(p);
        mDialog.getWindow().setBackgroundDrawable(null);
        mDialog.setCancelable(true);
        mDialog.setCanceledOnTouchOutside(true);

        mDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    /**
     * 重置窗口高度 TODO 用
     *
     * @param newDialogHeight
     */
    public void resetDialogHeight(int newDialogHeight) {
        Dialog mDialog = getDialog();
        WindowManager.LayoutParams p = mDialog.getWindow().getAttributes();
        p.height = newDialogHeight;
        mDialog.getWindow().setAttributes(p);
    }

    /**
     * 去边框
     */
    private void removeBorder() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(-1, -2);
        }
    }

    public abstract void init();

    /**
     * 是否隐藏底部栏
     */
    public void hideBottom() {
        isBottomHiden = true;
    }

    /**
     * 按钮 - 取消
     *
     * @param text
     * @param listener
     */
    public void setNegativeButton(String text, View.OnClickListener listener) {
        mDialogNegativeText = text;
        mDialogNegativeListener = listener;
    }

    /**
     * 按钮 - 提交
     *
     * @param text
     * @param listener
     */
    public void setPositiveButton(String text, View.OnClickListener listener) {
        mDialogPositiveText = text;
        mDialogPositiveListener = listener;
    }

    /**
     * 标题
     *
     * @param title
     */
    public void setTitle(String title) {
        mDialogTitle = title;
    }

    /**
     * 标题栏隐藏
     */
    public void hideTitle() {
        isTitleHiden = true;
    }

    /**
     * 添加窗体View
     *
     * @param view
     */
    public void addDialogBody(View view) {
        mContainerChildView = view;
    }

    /**
     * 空页面文本
     *
     * @param text
     */
    public void setEmptyText(String text) {
        mDialogEmptyContent.setText(text);
    }

    /**
     * 是否显示空图
     *
     * @param shown
     */
    public void setEmptyImageShown(boolean shown) {
        mDialogEmptyImage.setVisibility(shown ? View.VISIBLE : View.GONE);
    }

    /**
     * 是否显示空页面
     *
     * @param empty
     */
    public void showEmpty(boolean empty, boolean no_net, boolean net_slow) {
        mDialogEmpty.setVisibility(View.VISIBLE);
        emptyContainer.setVisibility(empty ? View.VISIBLE : View.GONE);
        noNetContainer.setVisibility(no_net ? View.VISIBLE : View.GONE);
        netSlowContainer.setVisibility(net_slow ? View.VISIBLE : View.GONE);
    }

    /**
     * 是否显示加载
     *
     * @param progress
     */
    public void showProgress(boolean progress) {
        mDialogProgress.setVisibility(progress ? View.VISIBLE : View.GONE);
    }

    public void setPositiveShown(boolean shown) {
        mFooterBtnPositive.setVisibility(shown ? View.VISIBLE : View.GONE);
    }

    public void setNegativeShown(boolean shown) {
        mFooterBtnNegative.setVisibility(shown ? View.VISIBLE : View.GONE);
    }

    public int getDialogHeight() {
        return dialogHeight;
    }

    public void addCloseButton() {
        mDialogHeaderClose.setVisibility(View.VISIBLE);
        mDialogHeaderClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    @OnClick({R.id.load_refresh, R.id.load_refresh_net})
    public void onClick(View view) {
        refresh();
    }

    protected void refresh() {

    }

}
